<?php

class make_html extends CI_Controller
{

	
	function __construct()
	{
	   parent::__construct();	

	}
	
	function index()
	{
		echo 'hello static html....';
		$this->load->database();
		$n=0;
		$cate_array = $this->db->select('*')->get('category')->result_array(); //获取文章分类
		

		for($i=0;$i<count($cate_array);$i++)
		{
		    $html = "";
			$num = 4;
			$n++;
			$counts = $this->db->where('category',$cate_array[$i]['ename'])->get('articles')->result_array();  //一个分类的总数
			$count_page = ceil(count($counts)/$num);  //当前一个分类可以分成几页
            echo $cate_array[$i]['ename'].":".$count_page;
		}
		echo "<br>".$n.":".count($cate_array);
	}
	
	public function make_list()
	{
		$this->load->database();
		$this->load->helper('url');
		
		$cate_array = $this->db->select('*')->get('category')->result_array(); //获取文章分类
		for($i=0;$i<count($cate_array);$i++)
		{
		    $html = "";
			$num = 4;
			$counts = $this->db->where('category',$cate_array[$i]['ename'])->get('articles')->result_array();  //一个分类的总数
			$count_page = ceil(count($counts)/$num);  //当前一个分类可以分成几页
            echo $cate_array[$i]['ename'].":".$count_page."<br>";
			for($p=0;$p<$count_page;$p++)
		    {
		  
		    	$offset = $p*$num;
		    	
			    
		    	$this->db->select('*')->where('category',$cate_array[$i]['ename']);  // 当前分类的内容，并且进行分页
			    $articles = $this->db->get('articles', $num,$offset)->result_array();
			    //echo $this->db->last_query()."<br>";
			   
			    $data['articles'] = $articles;
			    $data['count_page'] = $count_page;
			    $html = "";
			    $html.=$this->load->view('common/header','',TRUE);
			    $html.=$this->load->view('make_html/make_list',$data,TRUE);
			    $html.=$this->load->view('common/fooler','',TRUE);
			    $output = dirname(dirname(dirname(__FILE__)))."/".$cate_array[$i]['ename'];
			    if(! file_exists($output))
			    {
			    	mkdir($output);
			    }
			    
			    $n = $p+1;
			    $outname = $output."/list-".$n.".html";
			    $fp = fopen($outname,'w');
			    fwrite($fp, $html);
			    fclose($fp);
			   
		    }
		}
	}
	/* 生成文章文件  */
	public function make_page()
	{
		$this->load->database();
		$this->load->helper('url');
		
		$articles = $this->db->select('*')->get('articles')->result_array();
		  
		  $data['category'] = $this->db->select('*')->get('category')->result_array();
		  $data["comments"]  = $this->db->select('*')->get("comment")->result_array();   //获取最新评论

		foreach($articles as $row)
		{
			$data['comment'] = $this->db->select('*')->where('id',$row['id'])->get('comment')->result_array();
			 
			$html = "";
			$data['article'] = $row;
			$html.= $this->load->view('make_html/header','',TRUE);
			$html.= $this->load->view('make_html/show',$data,TRUE);
			$html.= $this->load->view('make_html/comment',$data,TRUE);
			$html.= $this->load->view('make_html/right',$data,TRUE);
			$html.= $this->load->view('make_html/footer','',TRUE);
			
			$output = dirname(dirname(dirname(__FILE__)))."/art/show-".$row['id'].".html";
			
			$fp = fopen($output,'w');
			fwrite($fp, $html);
			fclose($fp);
		}
	}

	/*
	public function make_index()
	{
	    $this->load->database();
		$this->load->helper('url');
		
		$articles = $this->db->select('*')->get('articles')->result_array();  
		  
		$data['category'] = $this->db->select('*')->get('category')->result_array();
		$data["comments"]  = $this->db->select('*')->get("comment")->result_array();   //获取最新评论
		$html = "";
		$html.= $this->load->view('make_html/header','',TRUE);
	    foreach($articles as $row)
		{

            $html.= '<div class="article" style="border-bottom:dotted 1px #ccc;">';
            $html.=' <div style="text-align:left;"><h2><a href="/ciwhy/art/show-'.$row['id'].'.html" alt="">'.$row['title'].'</a></h2></div>';
            $html.='<div style="text-align:left;">发布日期:<em>'.date('Y/m/d',$row['created']).'</em>&nbsp;  编辑:<em>'.$row['own'].'</em></div>';
            
            $html.='<div style="text-align:left; height:auto !important; height:100px; min-height:100px; line-height:18px;">';
            $html.= $row['body'];
            $html.='</div></div>';
			
	
		}
		$html.= $this->load->view('make_html/right',$data,TRUE);
		$html.= $this->load->view('make_html/footer','',TRUE);
		$output = dirname(dirname(dirname(__FILE__)))."/index.html";
			
		$fp = fopen($output,'w');
		fwrite($fp, $html);
		fclose($fp);
	}
*/
	/* 生成首页 index.html文件  */
	public function make_index()
	{
	    $this->load->database();
		$this->load->helper('url');
		
		$articles = $this->db->select('*')->get('articles')->result_array();  
		  
		$data['category'] = $this->db->select('*')->get('category')->result_array();
		$data["comments"]  = $this->db->select('*')->get("comment")->result_array();   //获取最新评论
		$html = "";
		$html.= $this->load->view('make_html/header','',TRUE);
	    foreach($articles as $row)
		{

            $html.= '<div class="article" style="border-bottom:dotted 1px #ccc;">';
            $html.=' <div style="text-align:left;"><h2><a href="/ciwhy/art/show-'.$row['id'].'.html" alt="">'.$row['title'].'</a></h2></div>';
            $html.='<div style="text-align:left;">发布日期:<em>'.date('Y/m/d',$row['created']).'</em>&nbsp;  编辑:<em>'.$row['own'].'</em></div>';
            
            $html.='<div style="text-align:left; height:auto !important; height:100px; min-height:100px; line-height:18px;">';
            $html.= $row['body'];
            $html.='</div></div>';
			
	
		}
		$html.= $this->load->view('make_html/right',$data,TRUE);
		$html.= $this->load->view('make_html/footer','',TRUE);
		$output = dirname(dirname(dirname(__FILE__)))."/index.html";
			
		$fp = fopen($output,'w');
		fwrite($fp, $html);
		fclose($fp);
	}
	
	public function makerss()
	{
		$this->load->library("makerss");
		
		$this->makerss->run();
	}

}











